/*
 * @lc app=leetcode.cn id=45 lang=javascript
 *
 * [45] 跳跃游戏 II
 */

// @lc code=start
/**
 * @param {number[]} nums
 * @return {number}
 */
var jump = function (nums) {
  const n = nums.length;
  if (n <= 1) return 0;
  // 当前可达右边界
  let edge = 0;
  let step = 0;
  // 当前范围可达最大右边界
  let max_reach = 0;
  for (let i = 0; i < n; i++) {
    max_reach = Math.max(nums[i] + i, max_reach);
    if (i === edge) {
      edge = max_reach;
      step++;
      if (edge >= n - 1) {
        break;
      }
    }
  }
  return step;
};
// @lc code=end
